package ${package}.service.demo;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.transaction.annotation.Transactional;
import ${package}.dao.demo.UserDAO;
import ${package}.entity.demo.User;
/**
 * 负责整个User模块内的业务的Facade.
 * 
 * 事务管理： 使用@Transactional定义所有方法为默认的事务关联策略(REQUIRE)和默认的隔离策略(DEFAULT).
 * 对只读方法，可进一步定义@Transactional(readOnly = true).
 * 在方法如果必须捕获处理异常，需重新抛出继承于RuntimeException的ServiceException触发Rollback
 * 
 * @author calvin
 */
@Transactional
public class UserManager {
	
	private UserDAO userDAO;

	@Transactional(readOnly = true)
	public User getUser(Integer id) {
		return userDAO.find(id);
	}

	public void createUser(User user) {
			userDAO.create(user);
	}

	public void updateUser(User user) {
		userDAO.update(user);
	}

	public void deleteUser(User user) {
		userDAO.delete(user);
	}

	/**
	 * 验证用户名密码.
	 * 
	 * @return 验证通过时返回true.用户名或密码错误均返回false.
	 */
	@Transactional(readOnly = true)
	public boolean auth(String loginName, String password) {
		if(StringUtils.isBlank(loginName)||StringUtils.isBlank(password))
			return false;
		
		return (userDAO.countByAuth(loginName, password) == 1);
	}
	
	@Required
	public void setUserDAO(UserDAO userDAO) {
		this.userDAO = userDAO;
	}
}
